package com.tansun.easycare.rule.rulegroup.model;

import java.util.List;
import java.util.Map;

public class RuleRefVOPage {
	
	private Integer pageNo;//页码
	private Integer pageSize=4;//每页数据条数
	private String projectCode;
	private String id;//规则id
	private String ruleGroupName;//关联的规则集名称
	private String ruleEntryName;//关联的条目名称
	private Map<String,Object> dataMap; 
	private List<?> list;
	private int length = 8;//页面下标数目
	private String funcName="page";//页面点击调用函数
	private int count;//一共多少条数据
	private boolean firstPage;
	private boolean lastPage;
	private int first;
	private int last;
	private int prev;
	private int slider=1;
	private int next;
	private String funcParam="";
	private String message="";
	private String html;
	
	public RuleRefVOPage() {
		super();
	}

	public RuleRefVOPage(RuleRefVOPage ruleRefVOPage,List<?> allRef) {
		super();
		pageNo = ruleRefVOPage.getPageNo()==null?1:ruleRefVOPage.getPageNo();
    	pageSize = ruleRefVOPage.getPageSize()==null?pageSize:ruleRefVOPage.getPageSize();
    	count = allRef.size();
    	if((pageNo-1)*pageSize>count) {
    		this.list = allRef;
    		pageNo = 1;
    	}else if(pageNo*pageSize>count) {
    		this.list = allRef.subList((pageNo-1)*pageSize, count);
    	}else {
    		this.list = allRef.subList((pageNo-1)*pageSize, pageNo*pageSize);
    	}
    	projectCode = ruleRefVOPage.getProjectCode();
    	initialize();
    	funcName = ruleRefVOPage.getFuncName();
    	funcParam = ruleRefVOPage.getFuncParam();
    	html= this.toString();
	}
	
	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public String getFuncName() {
		return funcName;
	}

	public void setFuncName(String funcName) {
		this.funcName = funcName;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public int getLast() {
		return last;
	}

	public void setLast(int last) {
		this.last = last;
	}

	public int getPrev() {
		return prev;
	}

	public void setPrev(int prev) {
		this.prev = prev;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	public List<?> getList() {
		return list;
	}
	public void setList(List<RuleRefVO> list) {
		this.list = list;
	}
	public String getRuleEntryName() {
		return ruleEntryName;
	}
	public void setRuleEntryName(String ruleEntryName) {
		this.ruleEntryName = ruleEntryName;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getProjectCode() {
		return projectCode;
	}
	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}
	public String getRuleGroupName() {
		return ruleGroupName;
	}
	public void setRuleGroupName(String ruleGroupName) {
		this.ruleGroupName = ruleGroupName;
	}
	
	public void initialize() {
		this.first = 1;
		this.last = (int) (this.count / (long) (this.pageSize < 1 ? 20 : this.pageSize) + (long) this.first - 1L);
		if (this.count % (long) this.pageSize != 0L || this.last == 0) {
			++this.last;
		}

		if (this.last < this.first) {
			this.last = this.first;
		}

		if (this.pageNo <= 1) {
			this.pageNo = this.first;
			this.firstPage = true;
		}

		if (this.pageNo >= this.last) {
			this.pageNo = this.last;
			this.lastPage = true;
		}

		if (this.pageNo < this.last - 1) {
			this.next = this.pageNo + 1;
		} else {
			this.next = this.last;
		}

		if (this.pageNo > 1) {
			this.prev = this.pageNo - 1;
		} else {
			this.prev = this.first;
		}

		if (this.pageNo < this.first) {
			this.pageNo = this.first;
		}

		if (this.pageNo > this.last) {
			this.pageNo = this.last;
		}

	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		if (this.pageNo == 1) {
			sb.append("<li class=\"disabled\"><a href=\"javascript:\">&#171; 上一页</a></li>\n");
		} else {
			sb.append("<li><a href=\"javascript:\" onclick=\"" + this.funcName + "(" + this.prev + "," + this.pageSize
					+ ",\'" + this.funcParam + "\');\">&#171; 上一页</a></li>\n");
		}

		int begin = this.pageNo - this.length / 2;
		if (begin < this.first) {
			begin = this.first;
		}

		int end = begin + this.length - 1;
		if (end >= this.last) {
			end = this.last;
			begin = end - this.length + 1;
			if (begin < this.first) {
				begin = this.first;
			}
		}

		int arg4;
		if (begin > this.first) {
			boolean i = false;

			for (arg4 = this.first; arg4 < this.first + this.slider && arg4 < begin; ++arg4) {
				sb.append("<li><a href=\"javascript:\" onclick=\"" + this.funcName + "(" + arg4 + "," + this.pageSize
						+ ",\'" + this.funcParam + "\');\">" + (arg4 + 1 - this.first) + "</a></li>\n");
			}

			if (arg4 < begin) {
				sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
			}
		}

		for (arg4 = begin; arg4 <= end; ++arg4) {
			if (arg4 == this.pageNo) {
				sb.append("<li class=\"active\"><a href=\"javascript:\">" + (arg4 + 1 - this.first) + "</a></li>\n");
			} else {
				sb.append("<li><a href=\"javascript:\" onclick=\"" + this.funcName + "(" + arg4 + "," + this.pageSize
						+ ",\'" + this.funcParam + "\');\">" + (arg4 + 1 - this.first) + "</a></li>\n");
			}
		}

		if (this.last - end > this.slider) {
			sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n");
			end = this.last - this.slider;
		}

		for (arg4 = end + 1; arg4 <= this.last; ++arg4) {
			sb.append("<li><a href=\"javascript:\" onclick=\"" + this.funcName + "(" + arg4 + "," + this.pageSize
					+ ",\'" + this.funcParam + "\');\">" + (arg4 + 1 - this.first) + "</a></li>\n");
		}

		if (this.pageNo == this.last) {
			sb.append("<li class=\"disabled\"><a href=\"javascript:\">下一页 &#187;</a></li>\n");
		} else {
			sb.append("<li><a href=\"javascript:\" onclick=\"" + this.funcName + "(" + this.next + "," + this.pageSize
					+ ",\'" + this.funcParam + "\');\">" + "下一页 &#187;</a></li>\n");
		}

		sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">当前 ");
		sb.append("<input type=\"text\" value=\"" + this.pageNo
				+ "\" onkeypress=\"var e=window.event||event;var c=e.keyCode||e.which;if(c==13)");
		sb.append(this.funcName + "(this.value," + this.pageSize + ",\'" + this.funcParam
				+ "\');\" onclick=\"this.select();\"/> / ");
		sb.append("<input type=\"text\" value=\"" + this.pageSize
				+ "\" onkeypress=\"var e=window.event||event;var c=e.keyCode||e.which;if(c==13)");
		sb.append(this.funcName + "(" + this.pageNo + ",this.value,\'" + this.funcParam
				+ "\');\" onclick=\"this.select();\"/> 条，");
		sb.append("共 " + this.count + " 条" + (this.message != null ? this.message : "") + "</a></li>\n");
		sb.insert(0, "<ul>\n").append("</ul>\n");
		sb.append("<div style=\"clear:both;\"></div>");
		return sb.toString();
	}

	public boolean isFirstPage() {
		return firstPage;
	}

	public void setFirstPage(boolean firstPage) {
		this.firstPage = firstPage;
	}

	public boolean isLastPage() {
		return lastPage;
	}

	public void setLastPage(boolean lastPage) {
		this.lastPage = lastPage;
	}

	public int getSlider() {
		return slider;
	}

	public void setSlider(int slider) {
		this.slider = slider;
	}

	public String getFuncParam() {
		return funcParam;
	}

	public void setFuncParam(String funcParam) {
		this.funcParam = funcParam;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}
	
}
